/**
 * 
 */
package com.momosw.games.jericho.board;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Stack;

import com.momosw.games.engine.GameConfiguration;
import com.momosw.games.engine.events.Event;

/**
 * <b>Project:</b> JerichoCardGame<br />
 * <b>Package:</b> com.momosw.games.jericho.board<br />
 * <b>Class:</b> Deck.java<br />
 * <br />
 * <i>GSI 2011</i><br />
 *
 * @author Miguel Coronado (miguelcb84@gmail.com)
 * @version	Aug 19, 2011
 *
 */
public class Deck extends Stack<Card> implements Event {

    /**  */
    private static final long serialVersionUID = -6035379379968003419L;

    /**
     * Constructor: create an empty deck
     */
    public Deck () {
        super();
    }
    
    /** 
     * Constructor
     * @param cards
     */
    public Deck (Collection<Card> cards) {
        super();
        this.addAll(cards);
    }
    
    public Deck (GameConfiguration config) {
        super();
        
        // Create te deck according to the configuration given
        Map<Card,Integer> deckConfig = config.getDeckConfig();
        for (Card card : deckConfig.keySet()){
            int multiplicity = deckConfig.get(card);
            do{
                this.push(card);
            }while (--multiplicity > 0);
        }
    }
    
    /** Shuffles the deck */
    public void shuffle () {
        
        List<Card> shuffle = new LinkedList<Card> ();
        shuffle.addAll(this);
        Collections.shuffle(shuffle);
        this.clear();
        this.addAll(shuffle);
        
    }

    public void shuffleScoringCards () {
        
        List<Card> shuffle = new LinkedList<Card> ();
        shuffle.addAll(this);
        Collections.shuffle(shuffle);
        this.clear();
        
        int tramo = shuffle.size()/6;
        int firstScore = tramo + new Random().nextInt(tramo);
        int secondScore = 3*tramo + new Random().nextInt(tramo);
        int thirdScore = 5*tramo + new Random().nextInt(tramo);
        
        for(int index = 0; index < shuffle.size(); index++){
            this.push(shuffle.get(index));
            if(index == firstScore || index == secondScore || index == thirdScore){
                this.push(new ScoringCard());
            }
        }
    }

    public Event hidePrivate() {
        return this;
    }
}
